﻿namespace ExpertNewsRotator2.ExpertNewsRotator2
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebPartPages;
    using System;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class CustomToolPart : ToolPart
    {
        private Button btnSearchSite = new Button();
        private DropDownList ddlBodyColumn = new DropDownList();
        private DropDownList ddlImageURLColumn = new DropDownList();
        private DropDownList ddlThumbImageURLColumn = new DropDownList();
        private DropDownList ddlTitleColumn = new DropDownList();
        private DropDownList ddlTypes = new DropDownList();
        private DropDownList ddlViews = new DropDownList();
        private Label lblEnd = new Label();
        private Label lblList = new Label();
        private Label lblOperator = new Label();
        private Label lblSite = new Label();
        private Label lblStart = new Label();
        private Label lblThumb = new Label();
        private Label lblViewFields = new Label();
        private TextBox oSiteURL = new TextBox();

        public CustomToolPart()
        {
            this.Title = "Please Select a Announcements List";
            this.oSiteURL.Text = SPContext.Current.Web.Url;
        }

        public override void ApplyChanges()
        {
            ExpertNewsRotator2 selectedWebPart = (ExpertNewsRotator2)base.ParentToolPane.SelectedWebPart;
            selectedWebPart.Text = this.ddlTypes.SelectedItem.Text;
            selectedWebPart.ListViewFields = this.ddlViews.SelectedItem.Text;
            if (this.oSiteURL.Text == string.Empty)
            {
                selectedWebPart.SiteURL = SPContext.Current.Site.Url;
            }
            selectedWebPart.SiteURL = this.oSiteURL.Text;
            selectedWebPart.TitleField = this.ddlTitleColumn.SelectedItem.Text;
            selectedWebPart.BodyField = this.ddlBodyColumn.SelectedItem.Text;
            selectedWebPart.ImageURLField = this.ddlImageURLColumn.SelectedItem.Text;
            selectedWebPart.ThumbImageURLField = this.ddlThumbImageURLColumn.SelectedItem.Text;
        }

        protected void btnSearchSite_Click(object sender, EventArgs e)
        {
            this.PopulatePageTypes();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            ExpertNewsRotator2 selectedWebPart = (ExpertNewsRotator2)base.ParentToolPane.SelectedWebPart;
            ListItem item = this.ddlTitleColumn.Items.FindByValue(selectedWebPart.TitleField);
            if (item != null)
            {
                item.Selected = true;
            }
            item = this.ddlBodyColumn.Items.FindByValue(selectedWebPart.BodyField);
            if (item != null)
            {
                item.Selected = true;
            }
            item = this.ddlImageURLColumn.Items.FindByValue(selectedWebPart.ImageURLField);
            if (item != null)
            {
                item.Selected = true;
            }
            item = this.ddlThumbImageURLColumn.Items.FindByValue(selectedWebPart.ThumbImageURLField);
            if (item != null)
            {
                item.Selected = true;
            }
        }

        protected void ddlTypes_SelectionChanged(object sender, EventArgs e)
        {
            this.populateViews();
        }

        protected void ddlViews_SelectionChanged(object sender, EventArgs e)
        {
            this.populateColumns();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.PopulateSiteURL();
            this.PopulatePageTypes();
            this.ddlTypes.CausesValidation = true;
            this.ddlTypes.AutoPostBack = true;
            this.ddlTypes.SelectedIndexChanged += new EventHandler(this.ddlTypes_SelectionChanged);
            this.btnSearchSite.Text = "Search Site";
            this.btnSearchSite.Click += new EventHandler(this.btnSearchSite_Click);
            this.ddlViews.CausesValidation = true;
            this.ddlViews.AutoPostBack = true;
            this.ddlViews.SelectedIndexChanged += new EventHandler(this.ddlViews_SelectionChanged);
            this.lblSite.Text = "Site name:";
            this.lblList.Text = "List name:";
            this.lblViewFields.Text = "List View:";
            this.lblOperator.Text = "News Title Column:";
            this.lblStart.Text = "News Body Column:";
            this.lblEnd.Text = "News Image URL Column:";
            this.lblThumb.Text = "News Thumb Image URL Column:";
            HtmlTable child = new HtmlTable();
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();
            cell.Controls.Add(this.lblSite);
            HtmlTableCell cell2 = new HtmlTableCell();
            cell2.Controls.Add(this.btnSearchSite);
            HtmlTableCell cell3 = new HtmlTableCell();
            cell3.Controls.Add(this.oSiteURL);
            row.Cells.Add(cell);
            row.Cells.Add(cell3);
            row.Cells.Add(cell2);
            HtmlTableRow row2 = new HtmlTableRow();
            HtmlTableCell cell4 = new HtmlTableCell();
            cell4.Controls.Add(this.lblList);
            HtmlTableCell cell5 = new HtmlTableCell();
            cell5.Attributes.Add("colspan", "2");
            cell5.Controls.Add(this.ddlTypes);
            row2.Cells.Add(cell4);
            row2.Cells.Add(cell5);
            HtmlTableRow row3 = new HtmlTableRow();
            HtmlTableCell cell6 = new HtmlTableCell();
            cell6.Controls.Add(this.lblViewFields);
            HtmlTableCell cell7 = new HtmlTableCell();
            cell7.Controls.Add(this.ddlViews);
            row3.Cells.Add(cell6);
            row3.Cells.Add(cell7);
            HtmlTableRow row4 = new HtmlTableRow();
            HtmlTableCell cell8 = new HtmlTableCell();
            cell8.Controls.Add(this.lblOperator);
            HtmlTableCell cell9 = new HtmlTableCell();
            cell9.Controls.Add(this.ddlTitleColumn);
            row4.Cells.Add(cell8);
            row4.Cells.Add(cell9);
            HtmlTableRow row5 = new HtmlTableRow();
            cell8 = new HtmlTableCell();
            cell8.Controls.Add(this.lblStart);
            cell9 = new HtmlTableCell();
            cell9.Controls.Add(this.ddlBodyColumn);
            row5.Cells.Add(cell8);
            row5.Cells.Add(cell9);
            HtmlTableRow row6 = new HtmlTableRow();
            cell8 = new HtmlTableCell();
            cell8.Controls.Add(this.lblEnd);
            cell9 = new HtmlTableCell();
            cell9.Controls.Add(this.ddlImageURLColumn);
            row6.Cells.Add(cell8);
            row6.Cells.Add(cell9);
            HtmlTableRow row7 = new HtmlTableRow();
            cell8 = new HtmlTableCell();
            cell8.Controls.Add(this.lblThumb);
            cell9 = new HtmlTableCell();
            cell9.Controls.Add(this.ddlThumbImageURLColumn);
            row7.Cells.Add(cell8);
            row7.Cells.Add(cell9);
            HtmlTableRow row8 = new HtmlTableRow();
            cell8 = new HtmlTableCell
            {
                InnerHtml = "<table><tr><td>Webpart by Courtenay Farquharson - <a href=\"http://www.sharepointings.com\">www.sharepointings.com</a></td></tr></table>"
            };
            row8.Cells.Add(cell8);
            child.Rows.Add(row);
            child.Rows.Add(row2);
            child.Rows.Add(row3);
            child.Rows.Add(row4);
            child.Rows.Add(row5);
            child.Rows.Add(row6);
            child.Rows.Add(row7);
            child.Rows.Add(row8);
            this.Controls.Add(child);
        }

        public void populateColumns()
        {
            if (this.ddlTypes.Items.Count > 0)
            {
                this.ddlTitleColumn.Items.Clear();
                this.ddlImageURLColumn.Items.Clear();
                this.ddlThumbImageURLColumn.Items.Clear();
                this.ddlBodyColumn.Items.Clear();
                SPSite site = new SPSite(this.oSiteURL.Text);
                SPUserToken userToken = SPContext.Current.Site.SystemAccount.UserToken;
                using (SPSite site2 = new SPSite(this.oSiteURL.Text, userToken))
                {
                    using (SPWeb web = site2.OpenWeb())
                    {
                        SPViewFieldCollection viewFields = web.Lists[this.ddlTypes.SelectedItem.Text].Views[this.ddlViews.SelectedItem.Text].ViewFields;
                        List<string> list = new List<string>();
                        foreach (string str in viewFields)
                        {
                            this.ddlTitleColumn.Items.Add(XmlConvert.DecodeName(str));
                            this.ddlBodyColumn.Items.Add(XmlConvert.DecodeName(str));
                            this.ddlImageURLColumn.Items.Add(XmlConvert.DecodeName(str));
                            this.ddlThumbImageURLColumn.Items.Add(XmlConvert.DecodeName(str));
                        }
                    }
                }
            }
        }

        public void PopulatePageTypes()
        {
            this.ddlTypes.Items.Clear();
            SPSite site = new SPSite(this.oSiteURL.Text);
            SPUserToken userToken = SPContext.Current.Site.SystemAccount.UserToken;
            using (SPSite site2 = new SPSite(this.oSiteURL.Text, userToken))
            {
                using (SPWeb web = site2.OpenWeb())
                {
                    SPListCollection lists = web.Lists;
                    foreach (SPList list in lists)
                    {
                        if (list.BaseTemplate == SPListTemplateType.Announcements)
                        {
                            this.ddlTypes.Items.Add(list.Title);
                        }
                    }
                }
            }
            this.populateViews();
        }

        public void PopulateSiteURL()
        {
            this.oSiteURL.Text = SPContext.Current.Web.Url;
        }

        public void populateViews()
        {
            if (this.ddlTypes.Items.Count > 0)
            {
                this.ddlViews.Items.Clear();
                SPSite site = new SPSite(this.oSiteURL.Text);
                SPUserToken userToken = SPContext.Current.Site.SystemAccount.UserToken;
                using (SPSite site2 = new SPSite(this.oSiteURL.Text, userToken))
                {
                    using (SPWeb web = site2.OpenWeb())
                    {
                        SPViewCollection views = web.Lists[this.ddlTypes.SelectedItem.Text].Views;
                        foreach (SPView view in views)
                        {
                            this.ddlViews.Items.Add(view.Title);
                        }
                    }
                }
                this.populateColumns();
            }
        }
    }
}

